package com.tronsis.distribution.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.tronsis.distribution.R;
import com.tronsis.distribution.activity.goods.ReceiptActivity;
import com.tronsis.distribution.activity.material.MyMaterialActivity;
import com.tronsis.distribution.activity.me.MessageActivity;
import com.tronsis.distribution.activity.me.WechatCardActivity;
import com.tronsis.distribution.bean.RulesMapBean;
import com.tronsis.distribution.bean.UserBean;
import com.tronsis.distribution.biz.factory.CommonFactory;
import com.tronsis.distribution.biz.factory.UserBizFactory;
import com.tronsis.distribution.fragment.GoodsFragment;
import com.tronsis.distribution.fragment.MaterialFragment;
import com.tronsis.distribution.fragment.MeFragment;
import com.tronsis.distribution.fragment.TeamTrainFragment;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.db.DBUtil;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.KeyboardUtils;
import com.tronsis.library.util.StringUtil;
import com.tronsis.library.util.ToastUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends ToolBarBaseActivity {

    @BindView(R.id.rb_material)
    RadioButton mRbMaterial;
    @BindView(R.id.rb_goods)
    RadioButton mRbGoods;
    @BindView(R.id.rb_team_train)
    RadioButton mRbTeamTrain;
    @BindView(R.id.rb_me)
    RadioButton mRbMe;
    @BindView(R.id.rg_state)
    RadioGroup mRgState;
    @BindView(R.id.ll_content)
    LinearLayout mLlContent;

    private Fragment mMaterialFragment;
    private Fragment mGoodsFragment;
    private Fragment mTeamTrainFragment;
    private Fragment mMeFragment;

    private Fragment mCurrentFragment;

    private String mWechatFid;

    private UserBean mUserBean;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        mUserBean = UserBizFactory.createBiz().getLoginUser(mContext);
        setTitle(false, getString(R.string.material_library));
        setRightTvText(getString(R.string.my_material));
        mRlRead.setVisibility(View.VISIBLE);
        mRlRead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, MessageActivity.class);
                startActivity(intent);
            }
        });


        initData();

        httpRulesMap();

        if (mUserBean == null) {
            return;
        }
        if (TextUtils.isEmpty(mUserBean.getIdCardNo())) {
            showDialog();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        httpGetUnread();


    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {
            mWechatFid = data.getStringExtra("wechatFid");
        }
    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        switch (paramId) {
            case RIGHT_TEXT:
                if (mCurrentFragment instanceof MaterialFragment) {
                    Intent intent = new Intent(mContext, MyMaterialActivity.class);
                    startActivity(intent);
                } else if (mCurrentFragment instanceof GoodsFragment) {
                    Intent intent = new Intent(mContext, ReceiptActivity.class);
                    startActivity(intent);
                }
                break;
            case LEFT_BUTTON:

                break;
        }

    }

    private void initData() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        mMaterialFragment = new MaterialFragment();

        mCurrentFragment = mMaterialFragment;
        ft.add(R.id.ll_content, mMaterialFragment);
        ft.commit();


        mRgState.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                httpGetUnread();
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                switch (checkedId) {
                    case R.id.rb_material:
                        if (mMaterialFragment == null) {
                            mMaterialFragment = new MaterialFragment();
                            ft.add(R.id.ll_content, mMaterialFragment);
                        } else {
                            if (mCurrentFragment == mMaterialFragment) {
                                return;
                            }
                            ft.show(mMaterialFragment);
                        }
                        if (mCurrentFragment != null) {
                            ft.hide(mCurrentFragment);
                        }
                        ft.commit();
                        mTvTitle.setText(R.string.material_library);
                        setRightTvText(getString(R.string.my_material));
                        mRlRead.setVisibility(View.VISIBLE);
                        mCurrentFragment = mMaterialFragment;
                        break;
                    case R.id.rb_goods:
                        if (mGoodsFragment == null) {
                            mGoodsFragment = new GoodsFragment();
                            ft.add(R.id.ll_content, mGoodsFragment);
                        } else {
                            if (mCurrentFragment == mGoodsFragment) {
                                return;
                            }
                            ft.show(mGoodsFragment);
                        }
                        if (mCurrentFragment != null) {
                            ft.hide(mCurrentFragment);
                        }
                        ft.commit();
                        mTvTitle.setText(R.string.goods_show);
                        setRightTvText(getString(R.string.receipt));
                        mCurrentFragment = mGoodsFragment;
                        break;
                    case R.id.rb_team_train:
                        if (mTeamTrainFragment == null) {
                            mTeamTrainFragment = new TeamTrainFragment();
                            ft.add(R.id.ll_content, mTeamTrainFragment);
                        } else {
                            if (mCurrentFragment == mTeamTrainFragment) {
                                return;
                            }
                            ft.show(mTeamTrainFragment);
                        }
                        if (mCurrentFragment != null) {
                            ft.hide(mCurrentFragment);
                        }
                        ft.commit();
                        mTvTitle.setText(R.string.distribution_train);
                        mTvRight.setVisibility(View.GONE);
                        mCurrentFragment = mTeamTrainFragment;
                        break;
                    case R.id.rb_me:
                        if (mMeFragment == null) {
                            mMeFragment = new MeFragment();
                            ft.add(R.id.ll_content, mMeFragment);
                        } else {
                            if (mCurrentFragment == mMeFragment) {
                                return;
                            }
                            ft.show(mMeFragment);
                        }
                        if (mCurrentFragment != null) {
                            ft.hide(mCurrentFragment);
                        }
                        ft.commit();
                        mTvTitle.setText(R.string.me);
                        mTvRight.setVisibility(View.GONE);
                        mCurrentFragment = mMeFragment;
                        break;
                    default:

                        break;
                }
            }
        });
    }

    private void showDialog() {
        View view = LayoutInflater.from(this).inflate(R.layout.dialog_person_info, null, false);
        final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
        final EditText cdCard = view.findViewById(R.id.et_cd_code);
        final EditText wechat = view.findViewById(R.id.et_wechat);
        final Button btnConfirm = view.findViewById(R.id.btn_confirm);
        LinearLayout qrCode = view.findViewById(R.id.ll_qr_code);
        qrCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, WechatCardActivity.class);
                intent.setType("main");
                intent.putExtra("wechatFid", mWechatFid);
                startActivityForResult(intent, 100);
            }
        });


        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String cdCodeStr = cdCard.getText().toString();
                if (StringUtil.isBlank(cdCodeStr)) {
                    ToastUtil.showMessage(mContext, "请输入身份证号码");
                    return;
                }
                if (!StringUtil.isIDNumber(cdCodeStr)) {
                    ToastUtil.showMessage(mContext, "请输入正确的身份证号码");
                    return;
                }
//                String wechatStr = wechat.getText().toString();
//                if (StringUtil.isBlank(wechatStr)) {
//                    ToastUtil.showMessage(mContext, "请输入微信号");
//                    return;
//                }
//
//                if (StringUtil.isBlank(mWechatFid)) {
//                    ToastUtil.showMessage(mContext, "请上传微信二维码");
//                    return;
//                }
                KeyboardUtils.hideSoftInput(MainActivity.this);
                httpUpdateUserInfo(cdCodeStr, null, null, dialog);
            }
        });

        dialog.setCancelable(false);
        dialog.show();
    }


    private void httpUpdateUserInfo(final String cdCodeStr, final String wechatStr, final String mWechatFid, final AlertDialog dialog) {
        try {

            UserBizFactory.createBiz().updateInfo(this, mUserBean.isAppNotice(), null, null, cdCodeStr, null, null, null, null, wechatStr, mWechatFid, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    mUserBean.setIdCardNo(cdCodeStr);
                    mUserBean.setWechatId(wechatStr);
                    mUserBean.setWechatVcardFid(mWechatFid);
                    DBUtil.getInstance(mContext).update(mUserBean);
                    dialog.dismiss();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }

    }

    private void httpRulesMap() {
        try {
            CommonFactory.createBiz().getRulesMap(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    RulesMapBean bean = (RulesMapBean) data;
                    bean.setId(1);
                    DBUtil.getInstance(mContext).save(bean);
                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void httpGetUnread() {
        try {
            UserBizFactory.createBiz().getUnread(this, new UICallBack() {
                @Override
                public void onStart() {

                }

                @Override
                public void onRefresh(Object data) {
                    UserBean loginUser = UserBizFactory.createBiz().getLoginUser(mContext);
                    boolean enableNotice = loginUser.isAppNotice();

                    Integer integer = (Integer) data;
                    if (integer != null) {
                        if (integer < 1) {
                            mTvUnRead.setText("0");
                            mTvUnRead.setVisibility(View.GONE);
                        }

                        if (integer > 0 && enableNotice) {
                            mTvUnRead.setVisibility(View.VISIBLE);
                            mTvUnRead.setText(String.valueOf(integer));
                            if (integer > 99) {
                                mTvUnRead.setText("..");
                            }
                        }
                    } else {
                        mTvUnRead.setVisibility(View.GONE);
                    }

                }

                @Override
                public void onFailure(int status) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
